﻿@{
    ViewData["Title"] = "Create";
    Layout = "~/Areas/Console/Views/Shared/_Layout.cshtml";
}
@section head{
    <script type="text/javascript">
        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'table', 'form'], function () {
            var $ = layui.$
                , form = layui.form
                , admin = layui.admin
                , table = layui.table
                , parentFrameIndex = parent.layer.getFrameIndex(window.name); //获取窗口索引;

            /**多语言初始化 */
            top.SetFormPageLanguage(layui);

            /* 监听提交 */
            form.on('submit(submit-form)', function (data) {
                var price = $("#price").val().trim().replace(/,/g, '');
                if (price == "") {
                    top.AlertByKey(layui, "enterOperationPrice", 0);
                    return false;
                } else if (isNaN(price)) {
                    top.AlertByKey(layui, "enterNotIsNumber", 0);
                    return false;
                } else if (Number(price) <= 0) {
                    top.AlertByKey(layui, "operationPriceThanZero", 0);
                    return false;
                }
                if ($("#remark").val() == "") {
                    top.AlertByKey(layui, "enterRemark", 0);
                    return false;
                }
                layui.layer.confirm(top.lang.confirmExecute, {
                    title: top.lang.balanceAdjustment,
                    btn: [top.lang.confirmBtn, top.lang.closeBtn]
                }, function (index, layero) {
                    var datas = {
                        "idList": data.field.idList,
                        "operationType": data.field.operationType,
                        "price": price,
                        "remark": data.field.remark
                    };
                    $.ajax({
                        type: "POST",
                        url: "/Console/MT5Users/EditBalanceAdjustmentItems",
                        data: datas, // 你的formid
                        async: true,
                        error: function (request) {
                            alert("Connection error");
                        },
                        beforeSend: function (xhr) {
                            layui.layer.load();
                        },
                        success: function (data) {
                            var item = eval(data);
                            if (item.code == 0) {
                                /**提示语弹框 */
                                top.ShowMessageByKey(layui, item.data, 6, 1000, function () {
                                    parent.layer.close(parentFrameIndex);
                                    parent.renderTable();
                                });
                            } else {
                                /**提示语弹框 */
                                top.AlertByKey(layui, item.data, 0);
                            }
                            layer.closeAll('loading');
                        }
                    });
                });
                return false;
            });
            //光标离开输入的金额四舍五入并保留2位小数
            $("#price").blur(function () {
                var obj = this.value;
                var mnyReg = /^([1-9][0-9]*|(([0]\.[0-9]{0,2}[1-9]\d*|[1-9][0-9]*\.\d*)))$/;//根据需求修改
                if (!(mnyReg.test(obj))) {
                    top.AlertByKey(layui, "enterNotIsNumber", 0);
                    return;
                }
                var n = 2; //保留得小数位
                obj = parseFloat(obj).toFixed(n);
                var left = obj.split(".")[0].split("").reverse();
                var right = obj.split(".")[1];
                var total = new Array();
                for (i = 0; i < left.length; i++) {
                    total.push(left[i]);
                    if ((i + 1) % 3 == 0 && (i + 1) != left.length) {
                        total.push(",");
                    }
                }
                $("#price").val(total.reverse().join("") + "." + right);
            });

            //光标进入，去除金额千分位，并去除小数后面多余的0
            $("#price").focus(function () {
                var oldMny = this.value.replace(/,/g, '');
                if (oldMny.indexOf(".") > 0) {
                    oldMny = oldMny.replace(/0+?$/, "");//去除尾部多余的0
                    oldMny = oldMny.replace(/[.]$/, "");//如果最后一位是.则去掉
                }
                this.value = oldMny;
            });
        });
    </script>
}

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="component-form-group">
                <div class="layui-form-item">
                    <label class="layui-form-label" lay-event="operationType">操作类型</label>
                    <div class="layui-input-block">
                        <input type="radio" name="operationType" value="0" title="入金" placeholder="cashDeposit" checked="checked">
                        <input type="radio" name="operationType" value="1" title="出金" placeholder="cashOut">
                        <input id="idList" name="idList" type="hidden" value="@ViewBag.idList" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" lay-event="operationPrice">操作金额</label>
                    <div class="layui-input-block">
                        <input type="text" name="price" id="price" placeholder="enterOperationPrice" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" lay-event="remark">备注</label>
                    <div class="layui-input-block">
                        <textarea name="remark" id="remark" placeholder="enterTypeRemark" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button class="layui-btn" lay-submit="" lay-filter="submit-form">立即提交</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>